package com.lgCore.serverHandler;

import com.lgCore.common.LocalSession;
import com.lgCore.nettyCocurrent.ExecutorScheduler;
import com.lgCore.nettyCocurrent.LoginCallable;
import com.lgCore.protoModel.ProtoMsg;
import com.lgCore.serverProcesser.HeartCallable;
import com.lgCore.util.ProtoByteUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

public class LoginRequestHandler extends ChannelInboundHandlerAdapter {
    private static final Logger log = LoggerFactory.getLogger(LoginRequestHandler.class);
    /**
     * 收到消息
     */
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        if (null == msg || !(msg instanceof ProtoMsg.Message)) {
            super.channelRead(ctx, msg);
            return;
        }

        ProtoMsg.Message message = (ProtoMsg.Message) msg;

        //取得请求类型
        ProtoMsg.LoginRequest loginRequest = message.getLoginRequest();
        if (!loginRequest.getType().equals(ProtoMsg.HeadType.LOGIN_REQUEST)) {
            super.channelRead(ctx, msg);
            return;
        }
        //登录功能
        LocalSession session = new LocalSession(ctx.channel());

        LoginCallable loginCallable = new LoginCallable(ctx,loginRequest,LoginRequestHandler.this);
        ExecutorScheduler.add(loginCallable);
//        //异步任务，处理登录的逻辑
//        CallbackTaskScheduler.add(new CallbackTask<Boolean>() {
//            @Override
//            public Boolean execute() throws Exception {
//                //登录默认通过
//                //装载信息
//                LocalSession session = new LocalSession(ctx.channel());
//                UserDTO user = new UserDTO();
//                user.setUserName(loginRequest.getUserName());
//                session.setUser(user);
//                session.setLogin(true);
//                SessionManger.addLocalSession(session);
//                //数据放到管道上，可共享
//                ctx.channel().attr(LocalSession.SESSION_KEY).set(session);
//                /**
//                 * 通知客户端：登录成功
//                 */
//                ProtoMsg.Message.Builder message = ProtoMsg.Message.newBuilder();
//                //登录返回
//                ProtoMsg.LoginResponse.Builder loginResponse= ProtoMsg.LoginResponse.newBuilder();
//                loginResponse.setClientId(session.getClientId());
//                loginResponse.setCode("200");
//                loginResponse.setResult(true);
//                loginResponse.setType(ProtoMsg.HeadType.LOGIN_RESPONSE);
//                message.setLoginResponse(loginResponse.build());
//                ctx.channel().writeAndFlush(ProtoByteUtil.protoToByte(message));
//
//                return true;
//            }
//
//            //异步任务返回
//            @Override
//            public void onBack(Boolean r) {
//                if (r) {
//                    ctx.pipeline().remove(LoginRequestHandler.this);
//                    log.info("登录成功:" + session.getUser());
//
//                } else {
//                    SessionManger.inst().closeSession(ctx);
//                    log.info("登录失败:" + session.getUser());
//                }
//            }
//            //异步任务异常
//
//            @Override
//            public void onException(Throwable t) {
//                SessionManger.inst().closeSession(ctx);
//                log.info("登录失败:" + session.getUser());
//
//            }
//        });

    }


}
